package com.cocoiko;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.cocoiko.customview.MapItemizedOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Map extends MapActivity implements OnClickListener,
		LocationListener {

	MapView mapview;
	MapController mapcontroller;
	List<Overlay> mapoverlays;
	ImageButton current_location_button;

	Context mapcontext;

	LocationManager locationManager;
	String bestprovider;

	double curLatitude;
	double curLongitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);

		current_location_button = (ImageButton) findViewById(R.id.current_location);
		current_location_button.setOnClickListener(this);

		mapview = (MapView) findViewById(R.id.map);
		mapview.setClickable(true);
		mapview.setBuiltInZoomControls(true);

		mapcontroller = mapview.getController();
		mapcontroller.setZoom(12);

		// get map overlays
		mapoverlays = mapview.getOverlays();

		// location manager
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		bestprovider = locationManager.getBestProvider(criteria, false);

		Location location = locationManager.getLastKnownLocation(bestprovider);
		setMapcenter(location);

	}

	public void showCocoiko() {

		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon);
		GeoPoint point = new GeoPoint((int) (curLatitude * 1000000),
				(int) (curLongitude * 1000000));
		addOverlayIntoMap(bitmap, "nghia", "nghialv.bk@gmail.com", point);

	}

	/*
	 * 
	 */
	public void addOverlayIntoMap(Bitmap bitmap, String name, String email,
			GeoPoint point) {
		Drawable drawable = new BitmapDrawable(bitmap);
		MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable,
				mapview.getContext());
		OverlayItem item = new OverlayItem(point, name, email);

		itemizedoverlay.addOverlay(item);
		mapoverlays.add(itemizedoverlay);
	}

	/*
	 * set map center
	 */
	public void setMapcenter(Location loc) {
		if (loc != null) {
			GeoPoint point = new GeoPoint((int) (loc.getLatitude() * 1000000),
					(int) (loc.getLongitude() * 1000000));
			mapcontroller.setCenter(point);
			mapcontroller.animateTo(point);
			mapcontroller.setZoom(12);
		}
	}

	/*
	 * set map center
	 */
	public void setMapCenter(double latitude, double longitude) {
		GeoPoint point = new GeoPoint((int) (latitude * 1000000),
				(int) (longitude * 1000000));
		mapcontroller.setCenter(point);
		mapcontroller.animateTo(point);
		mapcontroller.setZoom(12);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (item.getItemId()) {
		case R.id.home:
			if (!this.getClass().getName().equals(Home.class.getName())) {
				intent = new Intent(this, Home.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.friends:
			if (!this.getClass().getName().equals(Friends.class.getName())) {
				intent = new Intent(this, Friends.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.map:
			if (!this.getClass().getName().equals(Map.class.getName())) {
				intent = new Intent(this, Map.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.allkokoiko:
			if (!this.getClass().getName().equals(AllCocoiko.class.getName())) {
				intent = new Intent(this, AllCocoiko.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.message:
			if (!this.getClass().getName().equals(Message.class.getName())) {
				intent = new Intent(this, Message.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.setting:
			if (!this.getClass().getName().equals(Setting.class.getName())) {
				intent = new Intent(this, Setting.class);
				startActivity(intent);
				finish();
			}
			break;

		}

		return true;
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view == current_location_button) {
			setMapCenter(curLatitude, curLongitude);
			showCocoiko();
		}
	}

	@Override
	public void onLocationChanged(Location log) {
		// TODO Auto-generated method stub
		if (log != null) {
			curLatitude = log.getLatitude();
			curLongitude = log.getLongitude();
			setMapCenter(curLatitude, curLongitude);
		}
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		locationManager.requestLocationUpdates(bestprovider, 0, 0, this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		locationManager.removeUpdates(this);
	}
}
